<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="4" labelWidth="85" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <!-- 列筛选-->
    <!-- :fetchapi="fetchApi" -->

    <!--列操作-->
    <!-- <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <AjaxButton size="small" type="danger" :click="removeHandler">删除</AjaxButton>
      </template> -->
    <!-- </FilterTable> -->
    <div class="preview-wrap">
      <BasePrint ref="templateDealerSalesReport" :data="printList" template="templateDealerSalesReport" direction="horizontal" :isPreview="true" />
    </div>
    <BasePrint ref="templateDealerSalesReportPrint" :data="printList" template="templateDealerSalesReport" direction="horizontal" :isPreview="false" />
    <div style="text-align:right">
      <el-button type="primary" :loading="exportBtnLoading" @click="handlerPrint">打印</el-button>
    </div>
    <!--抽屉 -->
    <Drawer :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <QualityModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { getDealerSalesReport } from '@/api/repair/queryStatistics/queryAccounts';
import { notifyAction } from '../../../../utils';
// import { getUsedCardQualitys } from '@/api/usedcar/basic/qaproduct'; // 页面调用的的方法,对应的路径连接后台

export default {
  name: 'zxUsedCardQuality',
  components: {
    // QualityModal
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', delete: '删除', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: { xhrAbort: true },
      visible: false,
      visible1: false,
      dialogTitle: '',
      // fetchApi: getUsedCardQualitys,
      handleType: '',
      formData: {},
      tableData: [],
      ids: [],
      exportBtnLoading: false,
      printList: {}
    };
  },
  mounted() {
    // 分页
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'DATE',
          label: '请选择年月',
          fieldName: 'month',
          placeholder: '请选择...',
          dateType: 'month',
          initialValue: moment().format('YYYY-MM')
          // change: e => {
          //   this.isDisabled = true;

          //   console.log('e--', e);
          //   let yearMonth = e;
          //   let month = '';
          //   let year = '';
          //   if (!isEmpty(yearMonth)) {
          //     year = yearMonth.substring(0, 4);
          //     month = parseInt(yearMonth.substring(5, yearMonth.length));
          //   }
          //   this.uploadParams = {
          //     year: year,
          //     month: month
          //   };
          //   console.log('uploadParams--', this.uploadParams);
          // }
        }
      ];
    },
    // 点击打印
    async handlerPrint() {
      let month = this.topFilterList.find(x => x.fieldName == 'month').initialValue;
      if (!month || month == '') {
        notifyAction('请输入月份', 'warning');
        return;
      }
      this.exportBtnLoading = true;
      let res = await getDealerSalesReport({ month: month });
      if (res.resultCode == 200) {
        res.data.typeReport.forEach(x => {
          x.IN_OUT_NAME = this.createDictText(x.IN_OUT_TYPE, '8249');
        });
        this.printList = res.data;
        if (!this.printList.allReport) {
          this.printList.allReport = {};
        }
        if (!this.printList.partReport) {
          this.printList.partReport = {};
        }
        if (!this.printList.typeReport) {
          this.printList.typeReport = [];
        }
        this.printList.allReport.sumReceive = this.printList.allReport.sumReceive ? this.printList.allReport.sumReceive : 0;
        this.printList.allReport.partCostAmount = this.printList.allReport.partCostAmount ? this.printList.allReport.partCostAmount : 0;
        this.$nextTick(() => {
          this.$refs.templateDealerSalesReportPrint.EXCUTE_PRINT();
        });
      }

      this.exportBtnLoading = false;
    },
    selectData(val) {
      this.tableData = val;
    },
    addHandle() {
      // 新建
      this.handleType = 'add';
      this.showHandler();
    },
    // async removeHandler() {
    //   // 删除
    //   if (!this.tableData.length) {
    //     return this.$notify({
    //       title: '提示信息',
    //       message: '请选择数据',
    //       type: 'error'
    //     });
    //   }
    //   await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   });
    //   // console.log(999);
    //   await this.deleteDialog(); // 执行删除方法
    //   // console.log(999);
    // },
    // async deleteDialog() {
    //   // 取出数据id
    //   let res = {};
    //   this.ids = this.tableData.map(x => x.qualityId); // 获取选中页面的ID加到map集合中
    //
    //   res = await deleteUsedCardQualitys({ ids: this.ids.join(',') });
    //   // alert("aaaaaa"+res);
    //   // console.log(res);
    //   // 判断结果
    //   // alert("aaaaa"+res.resultCode+"aaa");
    //   if (res.resultCode === 200) {
    //     // alert(this.res);
    //     // console.log(res.data);
    //     if (res.data) {
    //       this.fetchParams = { ...this.fetchParams }; // 重复新加载
    //
    //       return this.$notify({
    //         // 提示信息
    //         title: '提示信息',
    //         message: '删除成功',
    //         type: 'success'
    //       });
    //     }
    //   }
    // },
    // editHandle(row) {
    //   this.handleType = 'edit'; // 编辑
    //   this.formData = row;
    //   this.showHandler();
    // },
    async changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
      let month = this.topFilterList.find(x => x.fieldName == 'month').initialValue;
      if (!month || month == '') {
        notifyAction('请输入月份', 'warning');
        return;
      }
      let res = await getDealerSalesReport({ month: month });
      if (res.resultCode == 200) {
        res.data.typeReport.forEach(x => {
          x.IN_OUT_NAME = this.createDictText(Number(x.IN_OUT_TYPE), '8249');
        });
        this.printList = res.data;
        if (!this.printList.allReport) {
          this.printList.allReport = {};
        }
        if (!this.printList.partReport) {
          this.printList.partReport = {};
        }
        if (!this.printList.typeReport) {
          this.printList.typeReport = [];
        }
        this.printList.allReport.sumReceive = this.printList.allReport.sumReceive ? this.printList.allReport.sumReceive : 0;
        this.printList.allReport.partCostAmount = this.printList.allReport.partCostAmount ? this.printList.allReport.partCostAmount : 0;
        this.$nextTick(() => {
          this.$refs.templateDealerSalesReport.EXCUTE_PRINT();
        });
      }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      // 显示
      this.dialogTitle = `${this.conf[this.handleType]}新车保养费用申请单`;
      this.visible = true;
    },
    closeHandler(val, type) {
      // 关闭
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
